/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.openmrs.module.basicmodule.DSS.Interpreter;

import org.openmrs.module.basicmodule.DSS.ast.AST;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Stephen
 */
public class AdditionOperator {

    private AST t;
    private InterpreterVisitor iv;

    public AdditionOperator(AST t, InterpreterVisitor iv) {
        this.t = t;
        this.iv = iv;
    }

    public Object execute() {
        Object operand1 = t.getKid(1).accept(iv);
        Object operand2 = t.getKid(2).accept(iv);
      
        try {
            if (operand1.getClass() == Class.forName("java.util.ArrayList")) {
                if (operand2.getClass() == Class.forName("java.util.ArrayList")) {
                    ((ArrayList)operand1).addAll((ArrayList)operand2);
                    return operand1;
                }
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AdditionOperator.class.getName()).log(Level.SEVERE, null, ex);
        }
        String type = InterpreterVisitor.parseType((String) operand1);

        if (type.equalsIgnoreCase("float")) {
            float result = Float.parseFloat((String) operand1) + Float.parseFloat((String) operand2);
            return Float.toString(result);
        } else if (type.equalsIgnoreCase("int")) {
            int result = Integer.parseInt((String) operand1) + Integer.parseInt((String) operand2);
            return Integer.toString(result);
        } else if (type.equalsIgnoreCase("string")) {
            return (String) operand1 + (String) operand2;
        }
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
